package com.doghome.news.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.doghome.news.annotation.CheckPermission;
import com.doghome.news.entity.User;
import com.doghome.news.service.UserService;
import com.doghome.news.utils.BizCode;
import com.doghome.news.utils.BizDto;
import com.github.pagehelper.PageInfo;

@RestController
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@PostMapping("/login")
	public BizDto<Map<String, Object>> login(@RequestBody Map<String, String> params){
		BizDto<Map<String, Object>> bizDto = new BizDto<Map<String,Object>>();
		
		Map<String, Object> map = userService.login(params.get("name"), params.get("pwd"));
		if(map == null) {
			bizDto.setCode(BizCode.ERROR);
			bizDto.setMsg("登录名或密码错误！");
		}else {
			bizDto.setCode(BizCode.SUCCESS);
			bizDto.setMsg("登录成功！");
			bizDto.setData(map);
		}
		return bizDto;
	}
	
	@GetMapping("/exit")
//	@CheckPermission(permission = {1,2})
	public BizDto<Boolean> exit(@RequestHeader(name = "Authorization") String auth) {
		BizDto<Boolean> bizDto = new BizDto<Boolean>();
		boolean flag = userService.loginOut(auth);
//		if(flag) {
//			bizDto.setCode(BizCode.SUCCESS);
//		}else {
//			bizDto.setCode(BizCode.ERROR);
//		}
		bizDto.setCode(BizCode.SUCCESS);
		bizDto.setData(flag);
		return bizDto;
	}
	
	@GetMapping("/nameRepeat")
	public BizDto<Boolean> nameRepeat(String uname){
		BizDto<Boolean> bizDto = new BizDto<Boolean>();
		bizDto.setCode(BizCode.SUCCESS);
		bizDto.setMsg("success");
		bizDto.setData(userService.repeatName(uname));
		return bizDto;
	}
	
	@PostMapping("/addUser")
	public BizDto<Boolean> addUser(@RequestBody User user){
		BizDto<Boolean> bizDto = new BizDto<Boolean>();
		user.setUtype(2);	//设置普通用户权限
		if(userService.addUser(user)) {
			bizDto.setCode(BizCode.SUCCESS);
			bizDto.setMsg("success");
			bizDto.setData(true);
		}else {
			bizDto.setCode(BizCode.ERROR);
			bizDto.setMsg("error");
			bizDto.setData(false);
		}
		return bizDto;
	}
	
	@GetMapping("/userByPage")
	@CheckPermission(permission = {1})
	public BizDto<PageInfo<User>> userByPage(@RequestParam Map<String, Object> params){
		if(!params.containsKey("pageNum")|| !params.containsKey("pageSize")) {
			return new BizDto<PageInfo<User>>(BizCode.ERROR, "页码或页容量不能为空！", null);
		}
		
		PageInfo<User> pageInfo = userService.userByPage(params);
		return new BizDto<PageInfo<User>>(BizCode.SUCCESS,"成功！",pageInfo);
	}
	
	@GetMapping("/getUserByToken")
	public BizDto<User> getUserByToken(@RequestHeader(name = "Authorization") String token){
		BizDto<User> bizDto = new BizDto<User>();
		bizDto.setCode(BizCode.SUCCESS);
		bizDto.setMsg("获取成功");
		bizDto.setData(token == null ? null : userService.userByToken(token));
		return bizDto;
	}
}
